<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        class myPromise{
            constructor(process){
                this.status = 'pending'
                this.msg = ''
                process(this.resolve.bind(this),this.reject.bind(this))
                return this
            }
            resolve(val){
                // 状态不可变
                if(this.status !== 'pending') return
                this.status = 'fulfilled'
                this.msg = val
            }
            reject(err){
                // 状态不可变
                if(this.status !== 'pending') return
                this.status = 'rejected'
                this.msg = err
            }
            then(fulfilled,rejected){
                if(this.status === 'fulfilled'){
                    fulfilled(this.msg)
                }
                if(this.status === 'rejected'){
                    rejected(this.msg)
                }
            }
        }
        let quan = new myPromise(function(resolve,reject){
            resolve('我是铨')
        })
        quan.then(function(success){
            console.log(success)
        },function(){
            console.log('fail')
        })

        // 真的
        let huan = new Promise(function(resolve,reject){
            // resolve('我是幻')
            reject()
        })
        huan.then((val)=>{
            console.log(val)
        },(err)=>{
            console.log('错误')
        })
    </script>
</body>
</html>